const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const porject = require('./config/project');
const config = require('./config');
const srcDir = path.resolve(
  process.cwd(),
  porject.name + '/src/' + config.staticName
);

function resolve(dir) {
  return path.join(__dirname, './', porject.name, dir);
}

function getEntry() {
  try {
    var jsPath = path.resolve(srcDir, 'js');
    var dirs = fs.readdirSync(jsPath);
    var matchs = [];
    var files = {};
    dirs.forEach((item) => {
      matchs = item.match(/(.+)\.js$/);
      if (matchs) {
        files[matchs[1]] = path.resolve(srcDir, 'js', item);
      }
    });
    // console.log(JSON.stringify(files))
  } catch (e) {
    console.log('没有', porject.name);
  }
  return files;
}

module.exports = {
  cache: true,
  entry: getEntry(),

  output: {
    path: __dirname + porject.name + '/dist/' + config.staticName + '/',
    filename: '[name].js',
  },

  resolve: {
    extensions: ['.js', '.jsx', '.json'],
    alias: {
      '@': resolve('src'),
    },
  },

  module: {
    rules: [
      {
        test: /\.js?$/,
        exclude: /node_modules/,
        use: ['babel-loader'],
      },
      {
        test: /\.scss$/,
        use: [
          { loader: 'style-loader' },
          { loader: 'css-loader' },
          { loader: 'sass-loader' },
          'postcss-loader',
        ],
      },
      {
        test: /\.(gif|png|jpe?g|svg)$/i,
        use: [
          {
            loader: 'url-loader',
            options: {
              name: '[name].[ext]?[hash]',
              limit: 10000,
            },
          },
        ],
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: 'fonts/[name].[hash:7].[ext]',
        },
      },
    ],
  },
};
